package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.Process;
import com.example.mapper.ProcessMapper;
import com.example.service.IProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 流程表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-04-10
 */
@Service
public class ProcessServiceImpl extends ServiceImpl<ProcessMapper, Process> implements IProcessService {

    @Override
    public ResultVo getProcessList(Integer classifyId, String keyword, Date start, Date end) {
        QueryWrapper<Process> wrapper = new QueryWrapper<>();
        wrapper.eq(classifyId!=null,"classify_id",classifyId)
                .and(keyword!=null,e->{ e.like("name",keyword).or().like("prefix",keyword);})
                .between(start!=null&&end!=null,"create_time",start,end);
        List<Process> processes = baseMapper.selectList(wrapper);
        return ResultVo.response(processes);
    }

    @Override
    public Boolean hasSmeNameSiblingProcess(Integer id, String name, Integer classifyId) {
        QueryWrapper<Process> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name)
                .eq("classify_id",classifyId)
                .notIn(id!=null,"id",id);
        Long aLong = baseMapper.selectCount(wrapper);
        return aLong>0;
    }

    @Override
    public Boolean hasSameCodeSiblingProcess(Integer id, String code) {
        QueryWrapper<Process> wrapper = new QueryWrapper<>();
        wrapper.eq("prefix",code)
                .notIn(id!=null,"id",id);
        Long aLong = baseMapper.selectCount(wrapper);
        return aLong>0;
    }
}
